//用户注册，登录，退出

// =============================注册================================
var registerUser = {
	userNameRegister : $("#userNameRegister"),
	passwordRegister : $("#passwordRegister"),
	passwordRegister2 : $("#passwordRegister2"),
	emailRegister : $("#emailRegister"),
	// cityRegister = $("#cityRegister");
	allFields : $([]).add(this.userNameRegister).add(this.passwordRegister)
			.add(this.passwordRegister2).add(this.emailRegister),
	tips : $(".validateTips"),
	updateTips : function(t) {
		tips.text(t).addClass("ui-state-highlight");
		setTimeout(function() {
			tips.removeClass("ui-state-highlight", 1500);
		}, 500);
	},
	checkLength : function(o, n, min, max) {
		if (o.val().length > max || o.val().length < min) {
			o.addClass("ui-state-error");
			updateTips("" + n + "的长度必须在" + min + "和" + max + "之间.");
			return false;
		} else {
			return true;
		}
	},
	checkRegexp : function(o, regexp, n) {
		if (!(regexp.test(o.val()))) {
			o.addClass("ui-state-error");
			updateTips(n);
			return false;
		} else {
			return true;
		}
	},
	// 检测返回数据是否正确
	successFunc : function(data) {
		if (data == "" || data == 0) {
			alert("<span color='red'>请求失败</span>");
		} else {
			if (data.resultCode != "1") {
				alert("请求失败，请求返回码：" + data.resultStr);
			} else {
				alert("请求成功");
				window.location.reload(true);
			}
		}
	},
	init : function() {
		$("#dialog-form-register").html('<p class="validateTips">字段都为必须字段</p>'+
		'<form>'+
		'	<fieldset>'+
		'<table><tr><td><label for="userNameRegister">用户名:</label></td>'+
		'		<td><input type="text" name="userNameRegister" id="userNameRegister" class="text ui-widget-content ui-corner-all" /></td></tr>'+
		'		<tr><td><label for="passwordRegister">密码设置:</label></td>'+
		'		<td><input type="password" name="passwordRegister" id="passwordRegister" class="text ui-widget-content ui-corner-all" /></td></tr>'+
		'		<tr><td><label for="passwordRegister2">确认密码:</label></td>'+
		'		<td><input type="password" name="passwordRegister2" id="passwordRegister2" class="text ui-widget-content ui-corner-all" /></td></tr>'+
		'		<tr><td><label for="customerRegister">所在城市:</label></td>'+
		'		<td><input type="radio" name="customerRegister" checked="checked" value="1"/>上海'+
		'		<input type="radio" name="customerRegister" value="2"/>北京'+
		'		<input type="radio" name="customerRegister" value="3"/>广州</td></tr>'+
		'		<tr><td><label for="emailRegister">电子邮件:</label></td>'+
		'		<td><input type="text" name="emailRegister" id="emailRegister" class="text ui-widget-content ui-corner-all" /></td></tr>'+
		'		<tr><td>验证码:</td>'+
		'		<td><span id="validationAreaHeaderR"><img alt="验证码" src="'+ROOTPATH+'/validateCode.aspx" style="width: 60px;height: 20px; vertical-align:middle;"></span>'+
		'		看不清,<a href="javascript:void(0)" onclick="reloadValidateCodeHeaderR();"><b>换一张</b></a></td>'+
		'		<tr><td>验证码输入:</td>'+
		'		<td><input type="text" size="10" id="validationCodeHeaderR"  name="validationCode" class="text ui-widget-content ui-corner-all"></td></tr>'+
		'</table>	</fieldset>'+
		'</form>');
		//初始化弹出框
		$("#dialog-form-register").dialog({
			autoOpen : false,
			height : 280,
			width : 350,
			modal : true,
			buttons : {
				"提交" : function() {
					// var bValid = true;
					// allFields.removeClass( "ui-state-error" );
					// if (cityId.val() == '' || cityId.val() == 0) {
					// alert(ROOTPATH+"/cityJson/addCity.action");
					/*
					 * $.post(ROOTPATH + "/cityJson/addCity.aspx", { cityName :
					 * cityName.val(), areaName : areaName.val(), areaNameDesc :
					 * areaNameDesc.val(), shortName : shortName.val() },
					 * function(data) { //alert(data); successFunc(data); });
					 */
					var cityId = 0;
					$('[name="customerRegister"]:radio').each(function() {
						if (this.checked) {
							cityId = this.value;
						}
					});
					if (cityId == 0) {
						alert("城市没有选择,请选择城市");
						return;
					} else {
					}
					var userName = $("#userNameRegister").val();
					var password = $("#passwordRegister").val();
					var password2 = $("#passwordRegister2").val();
					var email = $("#emailRegister").val();
					var validationCode = $("#validationCodeHeaderR").val();
					if (userName == "") {
						alert("用户名为空，请输用户名");
						return;
					}
					if (password == "") {
						alert("密码不能为空！");
						return;
					}
					//alert(password + "=" + password2);
					if (password != password2) {
						alert("两次输入的密码不一样，请重新输入");
						return;
					}
					if (email == "") {
						alert("电子邮件不能为空，请输入电子邮件");
						return;
					}
					if(validationCode== ""){
						alert("验证码不能为空，请输入验证码");
						return;
					}
					// private String userName;
					// private String passWord;
					// private String email;
					// private int cityId;

					$.post(ROOTPATH + "/customer/registeCustomer.aspx", {
						userName : userName,
						passWord : password,
						email : email,
						cityId : cityId,
						validationCode:validationCode
					}, function(data) {
						registerUser.successFunc(data);
					});

					// $(this).dialog("close");
				},
				"取消" : function() {
					$(this).dialog("close");
				}
			},
			"close" : function() {
				registerUser.allFields.val("").removeClass("ui-state-error");
			}
		});
	},
	registeCustomer : function() {
		$("#dialog-form-register").dialog("open");
	}
};
var loginUser = {
	userNameLogin : $("#userNameLogin"),
	passwordLogin : $("#passwordLogin"),
	// cityRegister = $("#cityRegister");
	allFields : $([]).add(this.userNameLogin).add(this.passwordLogin),
	tips : $(".validateTips"),
	updateTips : function(t) {
		tips.text(t).addClass("ui-state-highlight");
		setTimeout(function() {
			tips.removeClass("ui-state-highlight", 1500);
		}, 500);
	},
	// 检测返回数据是否正确
	successFunc : function(data) {
		if (data == "" || data == 0) {
			alert("<span color='red'>请求失败</span>");
		} else {
			if (data.resultCode != "1") {
				alert("请求失败，请求返回码：" + data.resultStr);
			} else {
				alert("登录成功");
				window.location.reload(true);
			}
		}
	},
	init : function() {
		
		$("#dialog-form-login").html('<p class="validateTips">字段都为必须字段</p>'+
		'<form>'+
		'	<fieldset>'+
		'		<table><tr><td><label for="userNameLogin">用户名:</label></td>'+
		'		<td><input type="text" name="userNameLogin" id="userNameLogin" class="text ui-widget-content ui-corner-all" /></td></tr>'+
		'		<tr><td><label for="passwordLogin">密码:</label></td>'+
		'		<td><input type="password" name="passwordLogin" id="passwordLogin" class="text ui-widget-content ui-corner-all" /></td></tr>'+
		'		<tr><td>验证码:</td>'+
		'		<td><span id="validationAreaHeader"><img alt="验证码" src="'+ROOTPATH+'/validateCode.aspx" style="width: 60px;height: 20px; vertical-align:middle;"></span>'+
		'		看不清,<a href="javascript:void(0)" onclick="reloadValidateCodeHeader();"><b>换一张</b></a></td>'+
		'		<tr><td>验证码输入:</td>'+
		'		<td><input type="text" size="10" id="validationCodeHeader"  name="validationCode" class="text ui-widget-content ui-corner-all"></td></tr>'+
		'		</table>'+
		'	</fieldset>'+
		'</form>');
		$("#dialog-form-login").dialog({
			autoOpen : false,
			height : 250,
			width : 320,
			modal : true,
			buttons : {
				"提交" : function() {
					var userName = $("#userNameLogin").val();
					var password = $("#passwordLogin").val();
					var validationCode = $("#validationCodeHeader").val();
					if (userName == "") {
						alert("用户名为空，请输用户名");
						return;
					}
					if (password == "") {
						alert("密码不能为空！");
						return;
					}
					if (validationCode == "") {
						alert("验证码不能为空！");
						return;
					}
					$.post(ROOTPATH + "/customer/login.aspx", {
						userName : userName,
						passWord : password,
						validationCode:validationCode
					}, function(data) {
						loginUser.successFunc(data);
					});

					// $(this).dialog("close");
				},
				"取消" : function() {
					$(this).dialog("close");
				}
			},
			"close" : function() {
				loginUser.allFields.val("").removeClass("ui-state-error");
			}
		});
	},
	login : function() {
		$("#dialog-form-login").dialog("open");
	}
};
//城市选择器
var citySelecter={
	init : function() {
		//初始化城市信息
		$.post(ROOTPATH + "/cityJson/findAllCity.aspx", {
		}, function(data) {
			//这里获得城市信息
			for(var i=0;i<data.list.length;i++){
				$("#city-selecter-citys").append('<input type="radio" name="city-selecter-input" value="'+data.list[i].id+'"/>'+data.list[i].cityName
						+'<input type="hidden" name="city-selecter-input-hidden" value="'+data.list[i].cityName+'" />');
			}
		});		
		$("#dialog-form-cityselecter").dialog({
			autoOpen : false,
			height : 300,
			width : 350,
			modal : true,
			buttons : {
				"提交" : function() {
					var cityId = 0;
					var cityName="";
					var index =0;
					$('[name="city-selecter-input"]:radio').each(function() {
						if (this.checked) {
							cityId = this.value;
							//cityName = this.class;
							//alert($('[name="city-selecter-input"]:hidden')[index].val());
							cityName = document.getElementsByName("city-selecter-input-hidden")[index].value;
						}
						index++;
					});
					if(cityId==0){
						alert("城市没有被选中,请选择");
						return;
					}
					$.post(ROOTPATH + "/cityJson/changeCity.aspx", {
						cityId : cityId
					}, function(data) {
						//loginUser.successFunc(data);
						if (data == "" || data == 0) {
							alert("请求失败，请联系管理员");
						} else {
							if (data.resultCode != "1") {
								alert("请求失败，请求返回码：" + data.resultStr);
							} else {
								alert("城市切换成功");
								//window.location.reload(true);
								//$("#dialog-form-cityselecter").dialog("close");
							}
						}
					});
					$(this).dialog("close");
					$("#currentCIty").html(cityName+"站");
					//window.location.reload(true);
				},
				"取消" : function() {
					$(this).dialog("close");
				}
			},
			"close" : function() {
				//loginUser.allFields.val("").removeClass("ui-state-error");
			}
		});
	},
	openDialog : function() {
		$("#dialog-form-cityselecter").dialog("open");
	}
};
$(function() {
	//注册用户dialog初始化
	registerUser.init();
	//城市dialog初始化
	citySelecter.init();
	// Initialize Menu instance(s):
	/*ddsmoothmenu.init({
		mainmenuid : "smoothmenu1", // menu DIV id
		orientation : 'v', // Horizontal or vertical menu: Set to "h" or "v"
		classname : 'nav'
	});*/
	loginUser.init();
});
function userLogout(){
	$.post(ROOTPATH + "/customer/logout.aspx", {
	}, function(data) {
		//loginUser.successFunc(data);
		if (data == "" || data == 0) {
			alert("请求失败");
		} else {
			if (data.resultCode != "1") {
				alert("请求失败，请求返回码：" + data.resultStr);
			} else {
				alert("退出成功");
				window.location.reload(true);
			}
		}
	});
}
function reloadValidateCodeHeader(){
	$("#validationAreaHeader").html("");
	$("#validationAreaHeader").html('<img alt="验证码" src="'+ROOTPATH+'/validateCode.aspx?a='+UUID.prototype.createUUID()+'" style="width: 60px;height: 20px; vertical-align:middle;">');
}
function reloadValidateCodeHeaderR(){
	
	$("#validationAreaHeaderR").html("");
	$("#validationAreaHeaderR").html('<img alt="验证码" src="'+ROOTPATH+'/validateCode.aspx?a='+UUID.prototype.createUUID()+'" style="width: 60px;height: 20px; vertical-align:middle;">');
}